﻿package net.pezzato.localization {
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	public class Translator {
		private static var langXml_s:XML;
		private static var ready_s:Boolean = false;
		private static var loader_s:TranslatorLoader = new TranslatorLoader();
		private static var lang_s:String;
		public static function loadLang(lang:String) {
			loader_s.addEventListener(TranslatorLoader.LOADED, onLangLoaded);
			loader_s.load(lang);
			lang_s = lang;
		}
		public static function translate(toTranslate:String):String {
			try {
				return (langXml_s[toTranslate][0]).toString();
			} catch (error:Error) {
				trace("Cannot translate " + toTranslate + " to " + lang_s);
			}
			return toTranslate;
		}
		public static function get loader():TranslatorLoader {
			return loader_s;
		}
		private static function onLangLoaded(event:Event) {
			langXml_s = event.target.langXml;
			ready_s = true;
			event.target.ready = true;
		}
	}
}